<template>
  <n-drawer v-model:show="isDrawer" :width="width" :placement="placement">
    <n-drawer-content :title="title" closable>
      <n-form
        :model="formParams"
        :rules="rules"
        ref="formRef"
        label-placement="left"
        :label-width="100"
      >
        <n-form-item label="上级国标码" path="parentId">
          <span>{{ parentId }}</span>
        </n-form-item>
        <n-form-item label="国标码" path="id">
          <n-input placeholder="请输入国标码" v-model:value="formParams.id" />
        </n-form-item>
        <n-form-item label="名称" path="name">
          <n-input placeholder="请输入名称" v-model:value="formParams.name" />
        </n-form-item>
        <n-form-item label="级别" path="level">
          <n-input-number
            :show-button="false"
            placeholder="请输入级别"
            v-model:value="formParams.level"
          />
        </n-form-item>
        <n-form-item label="经度" path="longitude">
          <n-input-number
            :show-button="false"
            placeholder="请输入经度"
            v-model:value="formParams.longitude"
          />
        </n-form-item>
        <n-form-item label="纬度" path="latitude">
          <n-input-number
            :show-button="false"
            placeholder="请输入纬度"
            v-model:value="formParams.latitude"
          />
        </n-form-item>
        <n-form-item label="排序" path="sequence">
          <n-input-number
            :show-button="false"
            placeholder="请输入排序"
            v-model:value="formParams.sequence"
          />
        </n-form-item>
        <n-form-item label="来源" path="source">
          <n-input type="textarea" placeholder="请输入来源" v-model:value="formParams.source" />
        </n-form-item>
      </n-form>

      <template #footer>
        <n-space>
          <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
          <n-button @click="handleReset">重置</n-button>
        </n-space>
      </template>
    </n-drawer-content>
  </n-drawer>
</template>

<script lang="ts">
  import { defineComponent, reactive, ref, toRefs } from 'vue';
  import { useMessage } from 'naive-ui';
  import { SaveOrUpdate } from './api';

  const rules = {
    id: {
      required: true,
      message: '请输入国标码',
      trigger: 'blur',
    },
    name: {
      required: true,
      message: '请输入名称',
      trigger: 'blur',
    },
  };
  export default defineComponent({
    name: 'CreateDrawer',
    components: {},
    props: {
      title: {
        type: String,
        default: '父级地区信息',
      },
      parentId: {
        type: Number,
        default: 0,
      },
      width: {
        type: Number,
        default: 450,
      },
    },
    emits: ['loadAreaTree'],
    setup(props, context) {
      const message = useMessage();
      const formRef: any = ref(null);
      const defaultValueRef = () => ({
        id: null,
        weight: null,
        parentId: props.parentId,
        name: null,
        value: null,
        label: null,
        level: null,
        longitude: null,
        latitude: null,
        sequence: null,
        source: null,
      });

      const state = reactive({
        isDrawer: false,
        subLoading: false,
        formParams: defaultValueRef(),
        placement: 'right',
      });

      function openDrawer() {
        state.isDrawer = true;
      }

      function closeDrawer() {
        state.isDrawer = false;
      }

      function formSubmit() {
        formRef.value.validate(async (errors) => {
          if (!errors) {
            state.formParams.parentId = props.parentId;
            await SaveOrUpdate(state.formParams).then(() => {
              message.success('操作成功', { duration: 3 });
            });
            handleReset();
            closeDrawer();
            context.emit('loadAreaTree');
          } else {
            message.error('请填写完整信息');
          }
        });
      }

      function handleReset() {
        formRef.value.restoreValidation();
        state.formParams = Object.assign(state.formParams, defaultValueRef());
      }

      return {
        ...toRefs(state),
        formRef,
        rules,
        formSubmit,
        handleReset,
        openDrawer,
        closeDrawer,
      };
    },
  });
</script>
